{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入pandas\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【例1】已知存放销售数据的excel文件的存储路径是\"D:\\销售数据.xlsx\"，请使用pandas的函数read_excel导入文件中的“销售”表。并观察下导入数据的基本情况。\n",
    "\n",
    "【分析】我们要使用pandas的函数read_excel()导入数据。根据之前的文件浏览，注意到“销售”表在excel文件中的第一个分页，参数sheetname可以是默认值0，也可以是分页的名称“销售”，文件中第一行文字是每个列的字段名，所以参数header也可以取默认值0，如果不需要调整字段名，则参数names也取缺省值。导入数据的观察，可以包括浏览下数据，可以用到方法head()，数据的规模，可以用到shape属性、数据的类型，可以使用dtypes属性等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_excel(r'd:\\销售数据.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>会员 ID</th>\n",
       "      <th>订单号</th>\n",
       "      <th>单价</th>\n",
       "      <th>运输成本</th>\n",
       "      <th>配送方式</th>\n",
       "      <th>产品分类</th>\n",
       "      <th>产品子分类</th>\n",
       "      <th>包裹大小</th>\n",
       "      <th>省(市)</th>\n",
       "      <th>下单日期</th>\n",
       "      <th>发货日期</th>\n",
       "      <th>利润</th>\n",
       "      <th>销售额</th>\n",
       "      <th>供应商ID</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>88525</td>\n",
       "      <td>2.88</td>\n",
       "      <td>0.50</td>\n",
       "      <td>汽车</td>\n",
       "      <td>办公用品</td>\n",
       "      <td>标签贴</td>\n",
       "      <td>小型箱子</td>\n",
       "      <td>广东</td>\n",
       "      <td>2012-05-28</td>\n",
       "      <td>2012-05-30</td>\n",
       "      <td>1.3200</td>\n",
       "      <td>5.90</td>\n",
       "      <td>S01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>88523</td>\n",
       "      <td>6.68</td>\n",
       "      <td>6.15</td>\n",
       "      <td>飞机</td>\n",
       "      <td>办公用品</td>\n",
       "      <td>打印纸</td>\n",
       "      <td>小型箱子</td>\n",
       "      <td>广东</td>\n",
       "      <td>2010-07-07</td>\n",
       "      <td>2010-07-08</td>\n",
       "      <td>4.5600</td>\n",
       "      <td>13.01</td>\n",
       "      <td>S02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>88523</td>\n",
       "      <td>205.99</td>\n",
       "      <td>2.50</td>\n",
       "      <td>飞机</td>\n",
       "      <td>电子产品</td>\n",
       "      <td>电话和通讯产品</td>\n",
       "      <td>小型箱子</td>\n",
       "      <td>广东</td>\n",
       "      <td>2011-07-27</td>\n",
       "      <td>2011-07-28</td>\n",
       "      <td>-47.6400</td>\n",
       "      <td>49.92</td>\n",
       "      <td>S02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>88526</td>\n",
       "      <td>120.97</td>\n",
       "      <td>26.30</td>\n",
       "      <td>火车</td>\n",
       "      <td>电子产品</td>\n",
       "      <td>办公设备</td>\n",
       "      <td>特大圆桶</td>\n",
       "      <td>广东</td>\n",
       "      <td>2011-07-27</td>\n",
       "      <td>2011-07-28</td>\n",
       "      <td>-30.5100</td>\n",
       "      <td>41.64</td>\n",
       "      <td>S03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3</td>\n",
       "      <td>88522</td>\n",
       "      <td>2.84</td>\n",
       "      <td>0.93</td>\n",
       "      <td>飞机</td>\n",
       "      <td>办公用品</td>\n",
       "      <td>笔类和相关类</td>\n",
       "      <td>超小型包裹</td>\n",
       "      <td>广东</td>\n",
       "      <td>2011-07-27</td>\n",
       "      <td>2011-07-27</td>\n",
       "      <td>998.2023</td>\n",
       "      <td>1446.67</td>\n",
       "      <td>S03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2573</th>\n",
       "      <td>996</td>\n",
       "      <td>89435</td>\n",
       "      <td>1.68</td>\n",
       "      <td>5.28</td>\n",
       "      <td>汽车</td>\n",
       "      <td>办公用品</td>\n",
       "      <td>胶水和夹子等</td>\n",
       "      <td>小型箱子</td>\n",
       "      <td>广东</td>\n",
       "      <td>2012-09-07</td>\n",
       "      <td>2012-09-07</td>\n",
       "      <td>-225.2505</td>\n",
       "      <td>36.16</td>\n",
       "      <td>S06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2574</th>\n",
       "      <td>997</td>\n",
       "      <td>89431</td>\n",
       "      <td>67.84</td>\n",
       "      <td>0.99</td>\n",
       "      <td>汽车</td>\n",
       "      <td>办公用品</td>\n",
       "      <td>办公电器</td>\n",
       "      <td>小型箱子</td>\n",
       "      <td>广东</td>\n",
       "      <td>2010-01-24</td>\n",
       "      <td>2010-01-29</td>\n",
       "      <td>-23.6344</td>\n",
       "      <td>63.66</td>\n",
       "      <td>S07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2575</th>\n",
       "      <td>998</td>\n",
       "      <td>89438</td>\n",
       "      <td>60.65</td>\n",
       "      <td>12.23</td>\n",
       "      <td>汽车</td>\n",
       "      <td>家具</td>\n",
       "      <td>办公家具</td>\n",
       "      <td>中型箱子</td>\n",
       "      <td>广东</td>\n",
       "      <td>2013-10-21</td>\n",
       "      <td>2013-10-23</td>\n",
       "      <td>379.2800</td>\n",
       "      <td>595.99</td>\n",
       "      <td>S07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2576</th>\n",
       "      <td>999</td>\n",
       "      <td>89433</td>\n",
       "      <td>45.19</td>\n",
       "      <td>1.99</td>\n",
       "      <td>汽车</td>\n",
       "      <td>电子产品</td>\n",
       "      <td>电脑外设</td>\n",
       "      <td>小型包裹</td>\n",
       "      <td>广东</td>\n",
       "      <td>2010-09-14</td>\n",
       "      <td>2010-09-15</td>\n",
       "      <td>-71.8300</td>\n",
       "      <td>127.22</td>\n",
       "      <td>S07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2577</th>\n",
       "      <td>1000</td>\n",
       "      <td>89433</td>\n",
       "      <td>33.98</td>\n",
       "      <td>19.99</td>\n",
       "      <td>汽车</td>\n",
       "      <td>家具</td>\n",
       "      <td>办公家具</td>\n",
       "      <td>小型箱子</td>\n",
       "      <td>广东</td>\n",
       "      <td>2010-09-14</td>\n",
       "      <td>2010-09-15</td>\n",
       "      <td>-0.7400</td>\n",
       "      <td>432.44</td>\n",
       "      <td>S07</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2578 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      会员 ID    订单号      单价   运输成本 配送方式  产品分类    产品子分类   包裹大小 省(市)       下单日期  \\\n",
       "0         2  88525    2.88   0.50   汽车  办公用品      标签贴   小型箱子   广东 2012-05-28   \n",
       "1         3  88523    6.68   6.15   飞机  办公用品      打印纸   小型箱子   广东 2010-07-07   \n",
       "2         3  88523  205.99   2.50   飞机  电子产品  电话和通讯产品   小型箱子   广东 2011-07-27   \n",
       "3         3  88526  120.97  26.30   火车  电子产品     办公设备   特大圆桶   广东 2011-07-27   \n",
       "4         3  88522    2.84   0.93   飞机  办公用品   笔类和相关类  超小型包裹   广东 2011-07-27   \n",
       "...     ...    ...     ...    ...  ...   ...      ...    ...  ...        ...   \n",
       "2573    996  89435    1.68   5.28   汽车  办公用品   胶水和夹子等   小型箱子   广东 2012-09-07   \n",
       "2574    997  89431   67.84   0.99   汽车  办公用品     办公电器   小型箱子   广东 2010-01-24   \n",
       "2575    998  89438   60.65  12.23   汽车    家具     办公家具   中型箱子   广东 2013-10-21   \n",
       "2576    999  89433   45.19   1.99   汽车  电子产品     电脑外设   小型包裹   广东 2010-09-14   \n",
       "2577   1000  89433   33.98  19.99   汽车    家具     办公家具   小型箱子   广东 2010-09-14   \n",
       "\n",
       "           发货日期        利润      销售额 供应商ID  \n",
       "0    2012-05-30    1.3200     5.90   S01  \n",
       "1    2010-07-08    4.5600    13.01   S02  \n",
       "2    2011-07-28  -47.6400    49.92   S02  \n",
       "3    2011-07-28  -30.5100    41.64   S03  \n",
       "4    2011-07-27  998.2023  1446.67   S03  \n",
       "...         ...       ...      ...   ...  \n",
       "2573 2012-09-07 -225.2505    36.16   S06  \n",
       "2574 2010-01-29  -23.6344    63.66   S07  \n",
       "2575 2013-10-23  379.2800   595.99   S07  \n",
       "2576 2010-09-15  -71.8300   127.22   S07  \n",
       "2577 2010-09-15   -0.7400   432.44   S07  \n",
       "\n",
       "[2578 rows x 14 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【例2】将上面的数据集中的“销售额”、“利润”这两列专门取出来，保存到一个新的excel文件。\n",
    "\n",
    "【分析】我们要使用DataFrame的方法to_excel()，将DataFrame对象的数据保存到excel文件中。首先是从上一例的DataFrame对象选取两列保存到一个新的DataFrame对象，接着方法to_excel()将新的对象存储到excel文件中，其中考虑设置参数index重新设置值为False，也就是行索引对象不需要保存到文件，参数sheet_name设置字符串“销售和利润”。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
